package com.txy.springcloud.service;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private ComboPooledDataSource dataSource;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public boolean insert(String email, String password, String token) throws SQLException {
        String now = sdf.format(new Date());

        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("insert into user(email, password, token, createTime, updateTime) values(?,?,?,?,?)");
        preparedStatement.setString(1, email);
        preparedStatement.setString(2, password);
        preparedStatement.setString(3, token);
        preparedStatement.setString(4, now);
        preparedStatement.setString(5, now);
        return preparedStatement.execute();

    }

//    public boolean checkVerificationCode(String code) throws SQLException {
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MINUTE, -10);
//        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("select * from verification_code where value = ? and createTime > ?");
//        preparedStatement.setString(1, code);
//        preparedStatement.setString(2, sdf.format(calendar.getTime()));
//        ResultSet resultSet = preparedStatement.executeQuery();
//        if (resultSet.next()) {
//            return true;
//        }
//        return false;
//    }

    public boolean checkPassword(String email, String password) throws SQLException {
        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("select * from user where email = ? and password = ?");
        preparedStatement.setString(1, email);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()) {
            return true;
        }
        return false;
    }

    public String getEmail(String token) throws SQLException {
        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("select * from user where token = ?");
        preparedStatement.setString(1, token);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()) {
            return resultSet.getString("email");
        }
        return "";
    }

    public boolean isRegistered(String email) throws SQLException {
        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("select * from user where email = ?");
        preparedStatement.setString(1, email);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()) {
            return true;
        }
        return false;
    }

    public boolean setToken(String email, String token) throws SQLException {
        PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement("update user set token = ? where email = ?");
        preparedStatement.setString(1, token);
        preparedStatement.setString(2, email);
        return preparedStatement.execute();
    }
}
